/* test3.c
 *
 * Copyright (C) 2007 Renato Araujo Oliveira Filho <renatox@gmail.com>
 * Copyright (C) 2007 Andre Moreira Magalhaes <andrunko@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include <goocanvas.h>

#include <math.h>

#include "gtktransition.h"

#define NUM_IMGS 12

static gboolean on_delete_event (GtkWidget *window,
                                 GdkEvent  *event,
                                 gpointer   unused_data);
static gdouble  _progress_func  (gdouble    progress);

int
main (int argc, char *argv[])
{
  GtkWidget *window, *canvas;
  GooCanvasItem *root, *item;
  GtkTransition *transition;
  GtkTransitionState *state;
  GdkPixbuf *pixbuf;
  GtkSettings *settings;
  int i;
  double x = 0;
  gboolean invert = FALSE;

  gtk_init (&argc, &argv);

  settings = gtk_settings_get_for_screen (gdk_screen_get_default ());
  g_object_set (settings, "gtk-enable-animations", TRUE, NULL);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_fullscreen (GTK_WINDOW (window));
  gtk_widget_show (window);

  g_signal_connect (window, "delete_event",
                    G_CALLBACK (gtk_main_quit),
                    NULL);

  canvas = goo_canvas_new ();
  g_object_set (G_OBJECT (canvas), "automatic-bounds", TRUE, NULL);
  gtk_widget_show (canvas);

  gtk_container_add (GTK_CONTAINER (window), canvas);

  root = goo_canvas_get_root_item (GOO_CANVAS (canvas));

  transition = gtk_transition_new (3000);

  pixbuf = gdk_pixbuf_new_from_file ("./icons/bg.png", NULL);
  item = goo_canvas_image_new (root, pixbuf, 0, 0, NULL);
  goo_canvas_item_add_child (root, item, -1);

  for (i = 1; i <= NUM_IMGS; ++i)
  {
    pixbuf = gdk_pixbuf_new_from_file ("./icons/icon.png", NULL);
    item = goo_canvas_image_new (root, pixbuf, x, 0.0, NULL);
    goo_canvas_item_add_child (root, item, -1);

    state = gtk_transition_state_new (G_OBJECT (item));
    gtk_transition_state_set (state,
                              "y", (invert ? 0.0 : 400.0), (invert ? 400.0 : 0.0),
                              NULL);
    gtk_transition_add_state (transition, state, NULL, 1.0);
    g_object_unref (state);

    x += 64.0;
    invert = !invert;
  }

  gtk_timeline_set_fps (GTK_TIMELINE (transition), 20);
  gtk_timeline_set_progress_func (GTK_TIMELINE (transition), _progress_func);
  gtk_timeline_set_loop (GTK_TIMELINE (transition), TRUE);
  gtk_timeline_start (GTK_TIMELINE (transition));

  gtk_main ();

  return 0;
}

static gboolean
on_delete_event (GtkWidget *window,
                 GdkEvent  *event,
                 gpointer   unused_data)
{
  gtk_main_quit ();
}

static gdouble
_progress_func (gdouble progress)
{
  return (sinf (progress * 2 * G_PI) / 2 ) + 0.5;
}
